/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.olap;

import mondrian.olap.Connection;
import mondrian.olap.DriverManager;

import mondrian.spi.impl.ServletContextCatalogLocator;

import java.io.Serializable;

import java.util.Locale;

import javax.servlet.ServletContext;


/**
 * Pivot settings.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 14-abr-2009 19:04:23
 */
public class PivotSettings implements Serializable {
	/**
	 * JNDI Prefix.
	 */
	public static final String JNDI_PREFIX = "java:comp/env/";

	/**
	 * The title.
	 */
	private String title;

	/**
	 * The print file name.
	 */
	private String printFile;

	/**
	 * The query.
	 */
	private String query;

	/**
	 * The data source.
	 */
	private String dataSource;

	/**
	 * The catalog uri.
	 */
	private String catalogUri;

	/**
	 * The schema processor.
	 */
	private String schemaProcessor;

	/**
	 * The use content checksum.
	 */
	private boolean useContentChecksum;

/**
   * Constructor.
   */
	public PivotSettings() {
	}

/**
   * Copy constructor.
   *
   * @param master the master.
   */
	@SuppressWarnings({"RedundantStringConstructorCall"})
	public PivotSettings(final PivotSettings master) {
		if(master != null) {
			this.title = master.title;
			this.printFile = master.printFile;
			this.query = master.query;
			this.dataSource = master.dataSource;
			this.catalogUri = master.catalogUri;
			this.schemaProcessor = master.schemaProcessor;
			this.useContentChecksum = master.useContentChecksum;
		}
	}

	/**
	 * Retrieves the title.
	 *
	 * @return the title.
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Sets the title.
	 *
	 * @param title the title.
	 */
	public void setTitle(final String title) {
		this.title = title;
	}

	/**
	 * Retrieves the print file.
	 *
	 * @return the print file.
	 */
	public String getPrintFile() {
		return printFile;
	}

	/**
	 * Sets the print file.
	 *
	 * @param printFile the print file.
	 */
	public void setPrintFile(final String printFile) {
		this.printFile = printFile;
	}

	/**
	 * Retrieves the query.
	 *
	 * @return the query.
	 */
	public String getQuery() {
		return query;
	}

	/**
	 * Sets the query.
	 *
	 * @param query the query.
	 */
	public void setQuery(final String query) {
		this.query = query;
	}

	/**
	 * Retrieves the data source.
	 *
	 * @return the data source.
	 */
	public String getDataSource() {
		return dataSource;
	}

	/**
	 * Sets the data source.
	 *
	 * @param dataSource the data source.
	 */
	public void setDataSource(final String dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * Retrieves the catalog uri.
	 *
	 * @return the catalog uri.
	 */
	public String getCatalogUri() {
		return catalogUri;
	}

	/**
	 * Sets the catalog uri.
	 *
	 * @param catalogUri the catalog uri.
	 */
	public void setCatalogUri(final String catalogUri) {
		this.catalogUri = catalogUri;
	}

	/**
	 * Retrieves the schema processor.
	 *
	 * @return the schema processor.
	 */
	public String getSchemaProcessor() {
		return schemaProcessor;
	}

	/**
	 * Sets the schema processor.
	 *
	 * @param schemaProcessor the schema processor.
	 */
	public void setSchemaProcessor(final String schemaProcessor) {
		this.schemaProcessor = schemaProcessor;
	}

	/**
	 * Get the use content checksum.
	 *
	 * @return the use content checksum.
	 */
	public boolean isUseContentChecksum() {
		return useContentChecksum;
	}

	/**
	 * Sets the use content checksum flag.
	 *
	 * @param useContentChecksum the use content checksum flag.
	 */
	public void setUseContentChecksum(boolean useContentChecksum) {
		this.useContentChecksum = useContentChecksum;
	}

	/**
	 * Builds the connect string.
	 *
	 * @param locale the locale.
	 *
	 * @return the connect string.
	 */
	protected String buildConnectString(final Locale locale) {
		StringBuilder builder = new StringBuilder("Provider=MONDRIAN;");

		builder.append("DataSource=").append(JNDI_PREFIX).append(dataSource).append(';');
		builder.append("Catalog=").append(catalogUri).append(';');
		builder.append("UseContentChecksum=").append(useContentChecksum).append(';');
		builder.append("UseSchemaPool=").append(true).append(';');
		builder.append("DynamicSchemaProcessor=").append(schemaProcessor).append(';');
		builder.append("Locale=").append(locale.toString()).append(';');

		return builder.toString();
	}

	/**
	 * Retrieves a connection.
	 *
	 * @param locale the locale.
	 * @param servletContext the servlet context.
	 *
	 * @return a connection.
	 */
	public Connection getConnection(final Locale locale, final ServletContext servletContext) {
		return DriverManager.getConnection(
			this.buildConnectString(locale), new ServletContextCatalogLocator(servletContext));
	}
}
